package com.rew.canteenchefs.controllers;

import com.rew.canteenchefs.models.EDishes;
import com.rew.canteenchefs.models.EUser;
import com.rew.canteenchefs.models.Result;
import com.rew.canteenchefs.services.imp.EDishesService;
import com.rew.canteenchefs.services.imp.EUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 *  维护端后台.
 *
 * Created by HuiWen Ren on 2016/12/17.
 */
@RestController
@RequestMapping("/canteenchefs/back")
public class EBAC {
    private static final Logger LOGGER = LoggerFactory.getLogger(ESTU.class);

    @Autowired
    EUserService userService;

    @Autowired
    EDishesService dishesService;

    /**
     * 菜品信息更新.
     *
     * @param dishes 更新菜品
     * @return 更新结果
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Result>
    updateDishes(@RequestBody EDishes dishes){
        LOGGER.info("receive request>>updateDishes: [{}], dishes");
        return ResponseEntity.ok(dishesService.updateDishes(dishes));
    }

    /**
     * 菜品添加.
     *
     * @param dishes 添加菜品
     * @return 更新结果
     */
    @RequestMapping(value = "/dishes/insert", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Result>
    addDishes(@RequestBody EDishes dishes){
        LOGGER.info("receive request>>insertDishes: [{}], dishes");
        return ResponseEntity.ok(dishesService.insertDishes(dishes));
    }


    /**
     * 菜品信息查询.
     *
     * @param dishName 菜品名称
     * @return 菜品信息
     */
    @RequestMapping(value = "/select/dish/{dishName}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Result<EDishes>>
    selectDishes(@PathVariable("dishName") String dishName){
        LOGGER.info("receive request>>selectDishes: [{}], dishName");
        return ResponseEntity.ok(dishesService.selectDishes(dishName));
    }

    /**
     * 菜品删除.
     *
     * @param dishName 菜品名称
     * @return 删除结果
     */
    @RequestMapping(value = "/delete/dish/{dishName}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Result<Integer>>
    deleteDishes(@PathVariable("dishName") String dishName){
        LOGGER.info("receive request>>deleteDishes: [{}], dishName");
        return ResponseEntity.ok(dishesService.deleteDishes(dishName));
    }

    /**
     * 用户添加.
     *
     * @param user 添加菜品
     * @return 更新结果
     */
    @RequestMapping(value = "/user/insert", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Result>
    addUser(@RequestBody EUser user){
        LOGGER.info("receive request>>insertUser: [{}], user");
        return ResponseEntity.ok(userService.insertUser(user));
    }

    /**
     * 个人用户信息查询.
     *
     * @param card_id 校园卡号
     * @return 用户信息
     */
    @RequestMapping(value = "/select/user/{card_id}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Result<EUser>>
    selectUser(@PathVariable("card_id") String card_id){
        LOGGER.info("receive request>>selectUser: [{}], card_id");
        return ResponseEntity.ok(userService.selectUser(card_id));
    }

    /**
     * 用户删除.
     *
     * @param card_id 用户id
     * @return 删除结果
     */
    @RequestMapping(value = "/delete/user/{card_id}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Result<Integer>>
    deleteUser(@PathVariable("card_id") String card_id){
        LOGGER.info("receive request>>deleteUser: [{}], card_id");
        return ResponseEntity.ok(userService.deleteUser(card_id));
    }
}
